package gns

import (
	"encoding/binary"
	"strings"
)

// buildDNSQuery 构建一个DNS查询数据包。
func buildDNSQuery(domain string) []byte {
	transactionID := 0x1234
	flags := 0x0100
	questions := 1
	answerRRs := 0
	authorityRRs := 0
	additionalRRs := 0
	var query []byte
	for _, part := range strings.Split(domain, ".") {
		query = append(query, byte(len(part)))
		query = append(query, part...)
	}
	query = append(query, 0)
	query = append(query, 0x00, 0x01)
	query = append(query, 0x00, 0x01)
	packet := make([]byte, 2+2+2+2+2+2+len(query))
	binary.BigEndian.PutUint16(packet[0:2], uint16(transactionID))
	binary.BigEndian.PutUint16(packet[2:4], uint16(flags))
	binary.BigEndian.PutUint16(packet[4:6], uint16(questions))
	binary.BigEndian.PutUint16(packet[6:8], uint16(answerRRs))
	binary.BigEndian.PutUint16(packet[8:10], uint16(authorityRRs))
	binary.BigEndian.PutUint16(packet[10:12], uint16(additionalRRs))
	copy(packet[12:], query)
	return packet
}
